package com.weitao.marketserver.controller;

import com.weitao.marketserver.mapper.ProductsMap;
import com.weitao.marketserver.pojo.Products;
import com.weitao.marketserver.pojo.ReturnData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/pro")
public class ProductsCon {
    @Autowired
    ProductsMap map;

    @Value("${user.file.path}")
    public String filePath;

    public Logger logger = LoggerFactory.getLogger(Products.class);

    @PostMapping("/updateitem")
    public ReturnData updateItem(Products param) {
        ReturnData returnData = new ReturnData();
        returnData.setCode(map.updateById(param));
        return returnData;
    }

    @PostMapping("/deleteitem")
    public ReturnData deleteItem(@RequestParam(value = "id") Integer id) {
        ReturnData returnData = new ReturnData();
        returnData.setCode(map.deleteById(id));
        return returnData;
    }

    @RequestMapping("/addnewpro")
    public ReturnData add(@RequestParam(value = "title", defaultValue = "未命名") String title,
                          @RequestParam(value = "price", defaultValue = "1.0") Float price,
                          @RequestParam(value = "fanli", defaultValue = "1") Float fanli,
                          @RequestParam(value = "sort", defaultValue = "1.0") Float sort,
                          @RequestParam(value = "fanlicishu", defaultValue = "1") Integer fanlicishu,
                          @RequestParam(value = "shengyukucun", defaultValue = "1") Integer shengyukucun,
                          @RequestParam(value = "zhuangtai", defaultValue = "1") Integer zhuangtai,
                          @RequestParam MultipartFile file) throws IOException {

        ReturnData returnData = new ReturnData();
        String newFileName = System.currentTimeMillis() + "fileName.jpg";
        String path = filePath + newFileName;

        if (file == null) {
            returnData.setCode(0);
            returnData.setMsg("请选择图片");
            return returnData;
        } else {
            File dest = new File(path);
            file.transferTo(dest);

            Products products = new Products();
            products.setTitle(title);
            products.setPrice(price);
            products.setFanli(fanli);
            products.setSort(sort);
            products.setFanlicishu(fanlicishu);
            products.setShengyukucun(shengyukucun);
            products.setZhuangtai(zhuangtai);
            products.setImage(newFileName);

            map.insert(products);

            returnData.setCode(1);
            returnData.setMsg("添加成功");
            return returnData;
        }
    }

    @RequestMapping("/all")
    public List<Products> all() {
        return map.selectList(null);
    }

}
